<!--  -->
<template>
  <div class="count-container">
    <div class="my-chart" ref="chart_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  props: {
    allData: {
      type: Array,
      default: null,
    },
  },
  data() {
    return {
      myChart: null,
      timer: null,
      seriesArr: [],
      currentIndex: 0,
      titleFontSize: 0,
    }
  },
  computed: {
    ...mapState(['theme']),
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
    this.startInterval()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
    clearInterval(this.timer)
  },
  methods: {
    initChart() {
      this.myChart = this.$echarts.init(this.$refs.chart_ref, this.theme)
      const initOption = {
        title: {
          text: '库存和销量',
          left: 'center',
          top: 15,
        },
      }
      this.myChart.setOption(initOption)
      this.myChart.on('mouseover', () => {
        clearInterval(this.timer)
      })
      this.myChart.on('mouseout', () => {
        this.startInterval()
      })
    },
    async getData() {
      if(this.allData) {
        this.updateChart()
      }
    },
    updateChart() {
      const colorArr = [
        ['#4ff778', '#0ba82c'],
        ['#e5dd45', '#e8b11c'],
        ['#e8821c', '#e55445'],
        ['#5052ee', '#ab6ee5'],
        ['#23e5e5', '#2e72bf'],
      ]
      const centerArr = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['33%', '75%'],
        ['66%', '75%'],
      ]
      let start = null
      let end = null
      if (this.allData.length > 5) {
        // console.log('???');
        start = this.currentIndex * 5
        end = (this.currentIndex + 1) * 5
      } else {
        start = 0
        end = this.allData.length
      }
      const showData = this.allData.slice(start, end)
      this.seriesArr = showData.map((item, index) => {
        return {
          type: 'pie',
          radius: [this.titleFontSize * 4, this.titleFontSize * 3.5],
          center: centerArr[index],
          hoverAnimation: false,
          labelLine: {
            show: false,
          },
          label: {
            position: 'center',
            color: colorArr[index][0],
          },
          data: [
            {
              value: Number(item.sales),
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  {
                    offset: 0,
                    color: colorArr[index][0],
                  },
                  {
                    offset: 1,
                    color: colorArr[index][1],
                  },
                ]),
              },
            },
            {
              value: item.stock,
              name:
                item.name.split(' ')[1] +
                '\n\n(' +
                item.sales +
                '/' +
                item.stock +
                ')',
              itemStyle: {
                color: '#d9d9d9',
              },
            },
          ],
        }
      })
      const dataOption = {
        series: this.seriesArr,
      }
      this.myChart.setOption(dataOption)
    },
    screenAdapter() {
      this.titleFontSize = (this.$refs.chart_ref.offsetWidth / 145) * 3.6
      const innerRadius = this.titleFontSize * 2
      const outterRadius = innerRadius * 1.125
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize * 1.5,
          },
        },
        series: [],
      }
      for (let i = 0; i < this.seriesArr.length; i++) {
        adapterOption.series.push({
          type: 'pie',
          radius: [outterRadius, innerRadius],
          label: {
            fontSize: this.titleFontSize / 5,
          },
        })
      }
      // console.log(adapterOption);
      this.myChart.setOption(JSON.parse(JSON.stringify(adapterOption)))
      this.myChart.resize()
    },
    startInterval() {
      if (this.timer) {
        clearInterval(this.timer)
      }
      this.timer = setInterval(() => {
        this.currentIndex++
        if (this.currentIndex > 1) {
          this.currentIndex = 0
        }
        this.getData()
        // this.updateChart()
      }, 5000)
    },
  },
}
</script>

<style lang="less" scoped></style>
